package cn.chenhao.pet.controller;

import cn.chenhao.basic.exception.BusinessException;
import cn.chenhao.basic.util.AjaxResult;
import cn.chenhao.basic.util.LoginContext;
import cn.chenhao.basic.util.PageList;
import cn.chenhao.pet.domain.Pet;
import cn.chenhao.pet.domain.SearchMasterMsg;
import cn.chenhao.pet.query.SearchMasterMsgQuery;
import cn.chenhao.pet.service.ISearchMasterMsgService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 寻主的发布接口
 */
@RestController
@Api(tags = "寻主接口",description = "寻主接口详细描述")// 接口说明，用在类上的
@RequestMapping("/searchMasterMsg")
public class SearchMasterMsgController {
    @Autowired
    private ISearchMasterMsgService seachMasterMsgService;


    @GetMapping
    public List<SearchMasterMsg> getAll(){
        return seachMasterMsgService.FindAll();
    }

    /**
     * 跳转到发布页面
     * @param msg
     * @param request
     * @return
     */
    @PostMapping("/publish")
    public AjaxResult publish(@RequestBody SearchMasterMsg msg, HttpServletRequest request){
        try {
            seachMasterMsgService.publish(msg, LoginContext.currentLoginInfo(request).getId());

            return AjaxResult.me();
        }
        catch (BusinessException exception) {
            exception.printStackTrace();
            throw new BusinessException("发布失败，你再想想应该怎么弄！");
        }
        catch (Exception exception) {
            exception.printStackTrace();
            return AjaxResult.me().setMessage("系统错误，稍后重试");
        }
    }

    /**
     * 待处理消息接口
     * @param searchMasterMsgQuery
     * @param request
     * @return
     */
    @PostMapping("/pending")
    public PageList<SearchMasterMsg> pending(@RequestBody SearchMasterMsgQuery searchMasterMsgQuery,HttpServletRequest request){
        try {
            return seachMasterMsgService.pending(searchMasterMsgQuery,LoginContext.currentLoginInfo(request).getId());
        }
        catch (Exception exception) {
            exception.printStackTrace();
            return new PageList<SearchMasterMsg>();
        }
    }

    /**
     * 已处理消息接口
     * @param searchMasterMsgQuery
     * @param request
     * @return
     */
    @PostMapping("/processed")
    public PageList<SearchMasterMsg> processed(@RequestBody SearchMasterMsgQuery searchMasterMsgQuery,HttpServletRequest request){
        try {
            return seachMasterMsgService.processed(searchMasterMsgQuery,LoginContext.currentLoginInfo(request).getId());
        }
        catch (Exception exception) {
            exception.printStackTrace();
            return new PageList<SearchMasterMsg>();
        }
    }

    /**
     * 登录人的个人中心接口
     */
    @PostMapping("/user")
    public PageList<SearchMasterMsg> queryUserMsg(@RequestBody SearchMasterMsgQuery searchMasterMsgQuery,HttpServletRequest request){
        try {
            return seachMasterMsgService.queryUserMsg(searchMasterMsgQuery,LoginContext.currentLoginInfo(request).getId());
        }
        catch (Exception exception) {
            exception.printStackTrace();
            return new PageList<SearchMasterMsg>();
        }
    }

    /**
     * 处理寻主消息
     * @param pet
     * @param request
     * @return
     */
    @PutMapping("/handle")
    public AjaxResult handle(@RequestBody Pet pet,HttpServletRequest request) {
        try {
            seachMasterMsgService.handle(pet,LoginContext.currentLoginInfo(request).getId());
            return AjaxResult.me();
        }
        catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("处理失败!" + e.getMessage());
        }
    }




}
